﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Secams.model
{
    /// <summary>
    /// Trials is a sequence o trials, where each trial is associated with a participant
    /// </summary>
    public class Trials
    {
        int _trialCtr = 0; // trial per version

        List<TrialProfile> _trialProfiles = new List<TrialProfile>();

        public TrialProfile addTrial(ParticipantInfo _participant, string scenarioSequenceName, string scenarioName, int mode)
        {
            if (_trialProfiles.Count > 0)
            {
                TrialProfile lastTrial = _trialProfiles.Last();
                // Reset counter if its a new scenarioSequence (version)
                if (!lastTrial.participant.id.Equals(_participant.id)
                    || !lastTrial.scenarioSequenceName.Equals(scenarioSequenceName))
                    _trialCtr = 0;
            }

            TrialProfile retval = new TrialProfile(_participant, ++_trialCtr);
            retval.scenarioSequenceName = scenarioSequenceName;
            retval.scenarioName = scenarioName;
            retval.mode = mode;
            _trialProfiles.Add(retval);
            return retval;
        }

        public List<TrialProfile> trialProfiles
        {
            get { return _trialProfiles; }
        }
    }

    /// <summary>
    /// Trial is a tuple of participant and a trial
    /// </summary>
    public class TrialProfile
    {
        public const int MODE_TRAINING = 1;
        public const int MODE_TRANSFER = 2;

        public string getModeText()
        {
            return (this.mode == TrialProfile.MODE_TRAINING) ? "Training" : "Transfer";
        }

        ParticipantInfo _participant = null;
        Trial _trial;
        string _scenarioSequenceName;
        string _scenarioName;
        int _mode; // training | transfer
        bool _decided = false; // decision made (i.e. Stopped)
        string _decision = null;
        string _decisionReason = null;
        int _decisionConfidence = 0;

        public TrialProfile(ParticipantInfo participant, int trialNum)
        {
            _participant = participant;
            _trial = new Trial(trialNum);
        }

        public ParticipantInfo participant
        {
            get { return _participant; }
        }

        public Trial trial
        {
            get { return _trial; }
        }

        public string scenarioSequenceName
        {
            get { return _scenarioSequenceName; }
            set { _scenarioSequenceName = value; }
        }
        public string scenarioName
        {
            get { return _scenarioName; }
            set { _scenarioName = value; }
        }
        public int mode
        {
            get { return _mode; }
            set { _mode = value; }
        }

        public bool decided
        {
            get { return _decided; }
            set { _decided = value; }
        }
        public string decision
        {
            get { return _decision; }
            set { _decision = value; }
        }

        public string decisionReason
        {
            get { return _decisionReason; }
            set { _decisionReason = value; }
        }

        public int decisionConfidence
        {
            get { return _decisionConfidence; }
            set { _decisionConfidence = value; }
        }
        
    }

    /// <summary>
    /// A trial is contains a sequence of events that the participants has processed
    /// </summary>
    public class Trial
    {
        protected int _trialNum = 0;
        List<EventProfile> _eventProfiles = new List<EventProfile>();

        public Trial(int trialNum)
        {
            _trialNum = trialNum;
        }

        public int trialNum
        {
            get { return _trialNum; }
            //set { _trialNum = value; }
        }
        public List<EventProfile> eventProfiles
        {
            get { return _eventProfiles; }
            //set { _trialNum = value; }
        }


        public void addEvent(NiEvent niEvent)
        {
            _eventProfiles.Add(new EventProfile(niEvent));
        }

        public void markThreathLabel(HashSet<int> sids)
        {
            foreach (EventProfile eventProfile in _eventProfiles)
            {
                if (sids.Contains(eventProfile.niEvent.sid))
                    eventProfile.labeledAsThreath = true;
            }
        }
    }



    public class EventProfile
    {
        protected NiEvent _niEvent;
        protected Boolean _labeledAsThreath = false;

        protected string _remark = null;

        public EventProfile(NiEvent niEvent)
        {
            this._niEvent = niEvent;
        }

        public NiEvent niEvent
        {
            get { return _niEvent; }
            set { _niEvent = value; }
        }

        public Boolean labeledAsThreath
        {
            get { return _labeledAsThreath; }
            set { _labeledAsThreath = value; }
        }

        public string remark
        {
            get { return _remark; }
            set { _remark = value; }
        }

    }
}
